﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using Rhino.Mocks.Interfaces;
using SKYPE4COMLib;
using SkypeStudio.Controls.Model;
using SkypeStudio.Controls.Services;
using SkypeStudio.Controls.Test.Mock;
using System;

namespace SkypeStudio.Controls.Test
{
    /// <summary>
    ///This is a test class for SkypeApplicationServiceTest and is intended
    ///to contain all SkypeApplicationServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class SkypeApplicationServiceTest : BaseTest
    {

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }




        /// <summary>
        ///A test for ClientStart
        ///</summary>
        [TestMethod()]
        public void ClientStartTest()
        {
            SKYPE4COMLib.Skype skype = _Mock.StrictMock<SKYPE4COMLib.Skype>();
            Client skypeClient = _Mock.StrictMock<Client>();
            SKYPE4COMLib.ChatMessage receiveMsg = _Mock.StrictMock<SKYPE4COMLib.ChatMessage>();
            SKYPE4COMLib.User sender = new SkypeUserMock();

            IEventRaiser messageStatusEventRaiser = null;

            using (_Mock.Record())
            {
                Expect.Call(skype.Client).Return(skypeClient).Repeat.Times(2);
                Expect.Call(skypeClient.IsRunning).Return(false);
                Expect.Call(delegate { skypeClient.Start(true, false); });
                Expect.Call(delegate { skype.Attach(); });

                Expect.Call(delegate { ((_ISkypeEvents_Event)skype).ConnectionStatus += null; }).IgnoreArguments();
                Expect.Call(delegate { ((_ISkypeEvents_Event)skype).AttachmentStatus += null; }).IgnoreArguments();
                messageStatusEventRaiser = Expect.Call(delegate { skype.MessageStatus += null; }).IgnoreArguments().GetEventRaiser();

                Expect.Call(receiveMsg.ChatName).Return("ChatName:Unitest").Repeat.Times(3);
                Expect.Call(receiveMsg.Sender).Return(sender).Repeat.Times(3);
                Expect.Call(receiveMsg.Body).Return("this is test message").Repeat.Times(3);
            }

            using (_Mock.Playback())
            {
                SkypeApplicationService target = new SkypeApplicationService(skype);
                bool ismessageReceived = false;
                bool ismessageSemding = false;
                bool ismessageSent = false;
                target.MessageReceived += (o) => { ismessageReceived = true; };
                target.MessageReceived += (o) => { ismessageSemding = true; };
                target.MessageReceived += (o) => { ismessageSent = true; };

                target.ClientStart();


                // Test Message Received
                messageStatusEventRaiser.Raise(receiveMsg, TChatMessageStatus.cmsReceived);
                Assert.IsTrue(ismessageReceived);

                messageStatusEventRaiser.Raise(receiveMsg, TChatMessageStatus.cmsSending);
                Assert.IsTrue(ismessageSemding);

                messageStatusEventRaiser.Raise(receiveMsg, TChatMessageStatus.cmsSent);
                Assert.IsTrue(ismessageSent);

            }
        }

        [TestMethod()]
        public void ClientShutdownTest()
        {
            SKYPE4COMLib.Skype skype = _Mock.StrictMock<SKYPE4COMLib.Skype>();
            Client skypeClient = _Mock.StrictMock<Client>();

            using (_Mock.Record())
            {
                Expect.Call(skype.Client).Return(skypeClient);
                Expect.Call(delegate { skypeClient.Shutdown(); });
            }

            using (_Mock.Playback())
            {
                SkypeApplicationService target = new SkypeApplicationService(skype);
                target.ClientShutdown();
            }
        }

        [TestMethod()]
        public void IsClientRunningTest()
        {
            SKYPE4COMLib.Skype skype = _Mock.StrictMock<SKYPE4COMLib.Skype>();
            Client skypeClient = _Mock.StrictMock<Client>();

            using (_Mock.Record())
            {
                Expect.Call(skype.Client).Return(skypeClient);
                Expect.Call(skypeClient.IsRunning).Return(true);
            }

            using (_Mock.Playback())
            {
                SkypeApplicationService target = new SkypeApplicationService(skype);
                var result = target.IsClientRunning;

                Assert.IsTrue(result);
            }
        }

        /// <summary>
        ///A test for GetContactList
        ///</summary>
        [TestMethod()]
        public void GetContactListTest()
        {
            SKYPE4COMLib.Skype skype = _Mock.StrictMock<SKYPE4COMLib.Skype>();
            //UserCollection usercollection = _Mock.StrictMock<UserCollection>();
            UserCollection usercollection = new UserCollectionMock();
            string alisase = "a";
            User userMock = new SkypeUserMock() { Aliases = alisase };
            usercollection.Add(userMock);

            using (_Mock.Record())
            {
                Expect.Call(skype.Friends).Return(usercollection);
            }

            using (_Mock.Playback())
            {
                SkypeApplicationService target = new SkypeApplicationService(skype);

                IEnumerable<Contact> actual = target.GetContactList();
                Assert.AreEqual(usercollection.Count, actual.ToList().Count());
                Assert.AreEqual(alisase, actual.ToList()[0].Aliases);
            }
        }

        //[TestMethod]
        //public void ImageResourceTest()
        //{
        //    var sk = new SkypeClass();
        //    SkypeApplicationService target = new SkypeApplicationService();
        //    target.ClientStart();

        //    IEnumerable<SkypeStudio.Controls.Model.Chat> actual = target.GetChatList();
        //    var list = actual.ToList();
        //}

        [TestMethod]
        public void GetChatListTest()
        {
            SKYPE4COMLib.Skype skype = _Mock.StrictMock<SKYPE4COMLib.Skype>();

           UserCollection usercollection = new UserCollectionMock();
            string alisase = "a";
            User userMock = new SkypeUserMock() { Aliases = alisase };
            usercollection.Add(userMock);

            ChatCollectionMock chatCollection = new ChatCollectionMock();
            ChatMessageCollection chatMessageCollection = (ChatMessageCollection)new ChatMessageCollectionMock();
            ChatClassMock chasClass = new ChatClassMock() { Members = usercollection, RecentMessages = chatMessageCollection };

            chatCollection.Add(chasClass);

            using (_Mock.Record())
            {
                Expect.Call(skype.RecentChats).Return(chatCollection);
                 Expect.Call(((ISkype)skype).AttachmentStatus).Return(TAttachmentStatus.apiAttachSuccess);
            }

            using (_Mock.Playback())
            {
                SkypeApplicationService target = new SkypeApplicationService(skype);

                IEnumerable<SkypeStudio.Controls.Model.Chat> actual = target.GetChatList();

                Assert.IsNotNull(actual);
                Assert.AreEqual(chatCollection.Count, actual.ToList().Count());
            }
        }

    }
}


